// pages/member/address.js
var util = require("../../../utils/util.js");

//获取应用实例
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      coupon_id: '',
      price: '',
      amount: '',
      desc: '',
      isSubmit: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        coupon_id: options.coupon_id,
      })

    },

    
    getInputValue(e) {
      let that = this;
      let value = e.detail.value;
      let type = e.currentTarget.dataset.type

      that.setData({
        [type]: value,
      })

    },

    onSubmit(e) {
      let that = this;
      let amount = that.data.amount;
      let price = that.data.price;
      if(amount.trim() == '' || price.trim() == ''|| that.data.desc.trim() == '') {
        app.tip('请填写完整');
        return ;
      }
      that.setData({
        isSubmit: true,
      })
      
      app.httpPost('member/coupon/use', {
        'amount': amount,
        'price': price,
        'coupon_id': that.data.coupon_id,
        'desc': that.data.desc
      }, json => {
        wx.redirectTo({
          url: '/pages/member/index',
        })
        if(json.data.error != 0) {
          app.tip(json.data.message)
        }else{
          app.tip('使用成功！')
        }
     
      })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

})